package com.example.demo.springcycle.createbean;

import com.example.demo.springcycle.MyBeanPostProcessor;
import com.example.demo.springcycle.entity.Dog;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

public class DogBeanDefinitionBuilder {

    public static void main(String[] args) {
        new Dog().say();
        System.out.println("=======");
        //准备生成Dog的定义
        final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Dog.class);
        //设置属性
        builder.addPropertyValue("name","john");
        builder.addPropertyValue("age",1);
        //builder.addDependsOn("cat");
        //builder.setScope("prototype");

        //生成BeanFactory
        final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerBeanDefinition("dog",builder.getBeanDefinition());
        final RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(MyBeanPostProcessor.class);
        beanFactory.registerBeanDefinition("myBeanPostProcessor",rootBeanDefinition);

        System.out.println("====bean已注册===");
        //获取dog
        Dog dogByName = (Dog)beanFactory.getBean("dog");
        dogByName.say();
        Dog dogByType = beanFactory.getBean(Dog.class);
        dogByType.say();
        System.out.println(dogByName == dogByType);
    }

}
